<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2016/12/21
 * Time: 9:40
 */

namespace SDXApi\Model;


use Think\Model;

/**
 * Class HomeTemplateStencilGoodsModel
 * @package SDXApi\Model
 */
class HomeTemplateStencilGoodsModel extends Model
{


	/**
	 * @var string
	 */
	protected $tableName = 'home_template_stencil_goods';


	/**
	 * Created by Jerry
	 * @param $stencil_id
	 * @return mixed
	 */
	public function find_template_stencil_goods_by_stencil_id($stencil_id)
	{
		$where = array(
			'stencil_id'	=>	$stencil_id,
		);

		$field = 'goods_id, sort, stencil_id';

		return $this->field($field)->where($where)->order('sort asc,id desc')->select();
	}

	/**
	 * Created by Jerry
	 * @param $stencil_id
	 * @param $wid
	 * @return bool
	 */
	public function findHomeTemplateGoodsByTemplateId($stencil_id, $wid)
	{

		if (empty($wid) || empty($stencil_id)) {
			return false;
		}

		$where = array(
			'sg.stencil_id'	=>	$stencil_id,
//			'gs.wid'		=>	$wid,
			'g.is_delete'	=>	0,
			'g.is_on_sale'	=>	1,
			'g.original_img'=>	array(
				'NEQ',
				''
			),
            'g.goods_img'=>	array(
                'NEQ',
                ''
            ),
		);

		$field = 'sg.goods_id,sg.sort,g.goods_name,g.goods_sn,g.goods_thumb,g.goods_img,g.goods_brief,g.is_presell,g.promote_price,g.promote_start_date,g.promote_end_date,g.goods_number,g.warn_number,g.original_img,g.is_on_sale,g.promote_goods_number,g.promote_today_number,g.is_buynow,g.spec_unit,g.spec_value,g.min_buy_num,g.delivery_type,g.delivery_cycle,gs.practical_num,g.promote_user_rank,g.is_promote';

		$data = $this
			->alias('sg')
			->field($field)
			->join('LEFT JOIN __GOODS_WID_' . $wid . '__ AS g ON sg.goods_id = g.goods_id')
			->join('LEFT JOIN __GOODS_STORAGE__ AS gs ON sg.goods_id = gs.goods_id AND gs.wid = ' . $wid)
			->where($where)
			->group('sg.goods_id')
			->select();

		return $data;
	}
}